<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="文件名称">
              <a-input placeholder="" v-model="queryParam.fileName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="文件分类">
              <a-select v-model="queryParam.fileType" placeholder="请选择文件分类">
                <a-select-option value="">全部</a-select-option>
                <a-select-option value="1">音频</a-select-option>
                <a-select-option value="2">视频</a-select-option>
                <a-select-option value="3">文档</a-select-option>
                <a-select-option value="4">图片</a-select-option>
                <a-select-option value="5">其他</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <template v-if="toggleSearchStatus">
            <a-col :md="12" :sm="24">
              <a-form-item label="上传时间" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
                <j-date v-model="queryParam.startTime" :showTime="true" date-format="YYYY-MM-DD"
                        placeholder="请选择开始时间"></j-date>
                <span style="width: 10px;">~</span>
                <j-date v-model="queryParam.endTime" :showTime="true" date-format="YYYY-MM-DD"
                        placeholder="请选择结束时间"></j-date>
              </a-form-item>
            </a-col>
          </template>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">上传文件</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="fileDown(record)">下载</a>&nbsp;&nbsp;
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <file-main-modal ref="modalForm" @ok="modalFormOk"></file-main-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import FileMainModal from './modules/FileMainModal'
import { httpAction } from '@api/manage'
import { message } from 'ant-design-vue'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { downloadFile } from '@api/uploadFile'

export default {
  name: 'FileMainList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    FileMainModal,

  },
  data() {
    return {
      description: '文件管理管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 30,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '文件名称',
          align: 'center',
          width: 210,
          dataIndex: 'fileName'
        },
        //(1音频、2视频、3文档、4图片、5其他)
        {
          title: '文件分类',
          align: 'center',
          width: 70,
          dataIndex: 'fileType',
          customRender: function (text) {
            if (text == '1') {
              return '音频'
            } else if (text == '2') {
              return '视频'
            } else if (text == '3') {
              return '文档'
            } else if (text == '4') {
              return '图片'
            } else {
              return '其他'
            }
          }
        },
        {
          title: '文件归档',
          align: 'center',
          width: 70,
          dataIndex: 'filing'
        },
        {
          title: '文件大小',
          align: 'center',
          width: 70,
          dataIndex: 'fileSizeName'
        },
        {
          title: '下载次数',
          align: 'center',
          width: 30,
          dataIndex: 'downCount',
          customRender: function (text) {
            if(text == null){
              return 0;
            }else {
              return text;
            }
          }
        },
        {
          title: '人员姓名',
          align: 'center',
          width: 70,
          dataIndex: 'createByName'
        },
        {
          title: '上传时间',
          align: 'center',
          width: 120,
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          // fixed: 'right',
          width: 100,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/file/main/list?commands=3',
        delete: '/file/main/true/delete',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getSuperFieldList()
  },
  methods: {
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'fileName', text: '文件名称' })
      fieldList.push({ type: 'int', value: 'fileType', text: '文件分类(1音频、2视频、3文档、4图片、5其他)' })
      fieldList.push({ type: 'number', value: 'fileSize', text: '文件大小(以bk为单位)' })
      fieldList.push({ type: 'string', value: 'fileSuffix', text: '文件后缀' })
      fieldList.push({ type: 'int', value: 'readCount', text: '阅读次数' })
      fieldList.push({ type: 'int', value: 'downCount', text: '下载次数' })
      fieldList.push({ type: 'string', value: 'filePath', text: '文件路径' })
      fieldList.push({ type: 'string', value: 'fileMd5', text: '文件MD5值' })
      fieldList.push({ type: 'string', value: 'createByName', text: '创建人姓名' })
      fieldList.push({ type: 'string', value: 'updateByName', text: '更新人姓名' })
      this.superFieldList = fieldList
    },
    async fileDown(record) {
      var elemIF = document.createElement('iframe')
      elemIF.style.display = 'none'
      elemIF.src = process.env.VUE_APP_API_BASE_URL + '/file/main/fileDown?id=' + record.id + '&filename=' + record.fileName + '&location=' + record.filePath + '&code=' + Vue.ls.get(ACCESS_TOKEN) + '&fileType=' + record.fileType + '&fileSize=' + record.fileSize + '&fileSuffix=' + record.fileSuffix
      document.body.appendChild(elemIF)
    },

  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>